package com.koron.ticket.operation.controller;

import com.koron.bean.base.Response;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.ticket.enums.OperationGroupEnum;
import com.koron.ticket.operation.bean.OperationStepBean;
import com.koron.ticket.operation.bean.OperationTicketBean;
import com.koron.ticket.operation.bean.WTicketSecurity;
import com.koron.ticket.operation.bean.WTicketSecuritySteps;
import com.koron.ticket.operation.service.OperationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("/securityTicketController")
@RestController
@Api(tags = "安全防误操作票")
public class SecurityTicketController {
    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }
    @Autowired
    OperationService operationservice;

    @ApiOperation(value = "新增更新")
    @PostMapping({"/update","/insert"})
    public Response update(@RequestBody WTicketSecurity wTicketSecurity){
        OperationTicketBean bean = new OperationTicketBean();
        setInfo(wTicketSecurity,bean);
        String runTask =ADOConnection.runTask(getCurrDs(), operationservice, "saveOrUpdate", String.class, bean);
        if (runTask.equals(wTicketSecurity.getObjId())){
            return Response.base(0,"","");
        }
        return Response.base(-1,"","");
    }

    private void setInfo(WTicketSecurity wTicketSecurity,OperationTicketBean operationTicketBean){
        operationTicketBean.setChecker(wTicketSecurity.getOperatingMan());
        operationTicketBean.setGuardian(wTicketSecurity.getTutelageMan());
        operationTicketBean.setIrritating(wTicketSecurity.getWrite());
        operationTicketBean.setTitle(wTicketSecurity.getTaskName());
        operationTicketBean.setId(wTicketSecurity.getObjId());
        operationTicketBean.setGroup(OperationGroupEnum.SecurityTicket.getValue());
        List<OperationStepBean> list = new ArrayList();
        for (WTicketSecuritySteps wTicketSecuritySteps : wTicketSecurity.getStepsList()) {
            OperationStepBean operationStepBean = new OperationStepBean();
            operationStepBean.setOperationId(wTicketSecurity.getObjId());
            operationStepBean.setSort(String.valueOf(wTicketSecuritySteps.getSequence()));
            operationStepBean.setItem(wTicketSecuritySteps.getContent());
            /*operationStepBean.setSbbh(wTicketSecuritySteps.getSbbh());
            operationStepBean.setFromState(wTicketSecuritySteps.getFromState());*/
            BeanUtils.copyProperties(wTicketSecuritySteps,operationStepBean);
            list.add(operationStepBean);
        }
        operationTicketBean.setStepdet(list);


    }
}
